package cn.wgd.zmx.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * json工具类
 */
public class JsonUtils {
    private static ObjectMapper objectMapper;

    /**
     * 对象转json
     * @param object
     */
    public static String pojoTojson(Object object) {
        objectMapper = new ObjectMapper();
        String str = null;
        try {
            str = objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * json转map对象
     */
    public static Map<String, Object> jsonTopojo(String jsonStr) {
        objectMapper = new ObjectMapper();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map = objectMapper.readValue(jsonStr, Map.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 其余的有待扩展
     */
}
